package Chat_program_Rasmus;

import javax.swing.*;
import java.net.*;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Image;
import java.awt.event.*;
import java.io.IOException;
import java.io.PrintWriter;


public class Client_GUI {
	
	// Globals
	private static Client ChatClient;
	public static String UserName = "Anonymous";
	
	//GUI Globals - Main Window
	
	public static JFrame MainWindow; 
	
	// Buttons
	private static JButton B_CONNECT;
	private static JButton B_DISCONNECT;
	private static JButton B_SEND;
	
	// Labels	
	private static JLabel L_Message;
	private static JLabel L_ONLINE;
	private static JLabel L_LoggedInAs;
	private static JLabel L_LoggedInAsBox;
	
	// Fields
	public static JTextField TF_Message;
	public static JTextArea TA_CONVERSATION;
	
	// ScrollPanels
	private static JScrollPane SP_CONVERSATION;
	private static JScrollPane SP_ONLINE;
	
	// List
	public static JList JL_ONLINE;
	
	// GUI Globals - LogIn Window
	public static JFrame LogInWindow;
	public static JTextField TF_UserNameBox;
	private static JButton B_ENTER;
	private static JLabel L_EnterUserName;
	private static JPanel P_LogIn;
	
	//------------------------------------------------------------------------------------
	
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					UIManager.setLookAndFeel("com.jtattoo.plaf.mcwin.McWinLookAndFeel");
					
					BuildMainWindow();
					Initialize();
					MainWindow.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});

	}
	
	//------------------------------------------------------------------------------------
	
	public static void Connect()
	{
		try
		{
			final int PORT = 9876;
			Socket SOCK = new Socket("localhost",PORT);
			System.out.println("You are now connected");
			
			ChatClient = new Client(SOCK);
			
			// Send Name to Online list
			PrintWriter OUT = new PrintWriter(SOCK.getOutputStream());
			OUT.println(UserName);
			OUT.flush();
			
			Thread X = new Thread(ChatClient);
			X.start();
		}
		catch(Exception X)
		{
			System.out.println(X);
			JOptionPane.showMessageDialog(null, "Server not responding");
			System.exit(0);
		}	
	}
	
	//------------------------------------------------------------------------------------
	
	public static void Initialize()
	{
		ImageIcon ICON_SEND = new ImageIcon("C:\\Users\\Rasmus Friis\\workspace\\Programmer\\src\\Chat_program_Rasmus\\MSN Messenger - Conversation 2.png"); 
	    ICON_SEND.setImage(ICON_SEND.getImage().getScaledInstance(20,20,Image.SCALE_DEFAULT));
	    B_SEND.setIcon(ICON_SEND);
		B_SEND.setEnabled(false);
		
		ImageIcon ico = new ImageIcon("C:\\Users\\Rasmus Friis\\workspace\\Programmer\\src\\Chat_program_Rasmus\\disconnect.png"); 
	    ico.setImage(ico.getImage().getScaledInstance(20,20,Image.SCALE_DEFAULT));
		B_DISCONNECT.setIcon(ico);
		B_DISCONNECT.setEnabled(false);
		
		ImageIcon ico2 = new ImageIcon("C:\\Users\\Rasmus Friis\\workspace\\Programmer\\src\\Chat_program_Rasmus\\connect.png");
		ico2.setImage(ico2.getImage().getScaledInstance(20,20,Image.SCALE_DEFAULT));
		B_CONNECT.setIcon(ico2);
		B_CONNECT.setEnabled(true);	
		
	}
	
	//------------------------------------------------------------------------------------
	
	public static void BuildLogInWindow()
	{
		LogInWindow = new JFrame();
		LogInWindow.setTitle("What is your name?");
		LogInWindow.setSize(400, 100);
		LogInWindow.setLocation(250, 200);
		LogInWindow.setResizable(false);
		
		TF_UserNameBox = new JTextField(20);
		B_ENTER = new JButton("Log User In");
		L_EnterUserName = new JLabel("Enter Username: ");
		
		P_LogIn = new JPanel();
		P_LogIn.add(L_EnterUserName);
		P_LogIn.add(TF_UserNameBox);
		P_LogIn.add(B_ENTER);
		LogInWindow.add(P_LogIn);
		
		Login_Action();
		LogInWindow.setVisible(true);
		
	}

	//------------------------------------------------------------------------------------
	
	public static void BuildMainWindow()
	{
		MainWindow = new JFrame();
		MainWindow.setTitle(UserName + "'s chat Box");
		MainWindow.setSize(500,320);
		MainWindow.setLocation(220, 180);
		MainWindow.setResizable(false);
		MainWindow.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		MainWindow.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent windowEvent) {
				try {
					ChatClient.DISCONNECT();
					MainWindow.dispose();
				} catch (IOException e) { e.printStackTrace(); }
			}
		});
		
		ConfigureMainWindow();	
	}
	
	//------------------------------------------------------------------------------------
	
	public static void ConfigureMainWindow()
	{
		//MainWindow.setBackground(new java.awt.Color(255,255,255));
		MainWindow.setSize(500,320);
		MainWindow.getContentPane().setLayout(null);
		
		B_SEND = new JButton();
		B_SEND.setText("Send");
		MainWindow.getContentPane().add(B_SEND);
		B_SEND.setBounds(250, 40, 81, 25);
		
		B_DISCONNECT = new JButton();
		B_DISCONNECT.setText("Disconnect");
		MainWindow.getContentPane().add(B_DISCONNECT);
		B_DISCONNECT.setBounds(10, 40, 110,25);
		
		B_CONNECT = new JButton();
		B_CONNECT.setText("Connect");
		B_CONNECT.setToolTipText("Connect");
		MainWindow.getContentPane().add(B_CONNECT);
		B_CONNECT.setBounds(130, 40, 110, 25);
		
		L_Message = new JLabel("Message: ");
		L_Message.setText("Message: ");
		MainWindow.getContentPane().add(L_Message);
		L_Message.setBounds(10, 10,60, 20);
					
		TF_Message = new JTextField(20);
		TF_Message.setBackground(new java.awt.Color(255,255,255));
		TF_Message.requestFocus();
		MainWindow.getContentPane().add(TF_Message);
		TF_Message.setBounds(70, 4, 260, 30);
		
		TA_CONVERSATION = new JTextArea();
		TA_CONVERSATION.setColumns(20);
		TA_CONVERSATION.setFont(new java.awt.Font("Tahoma",0,15));
		TA_CONVERSATION.setForeground(new java.awt.Color(0,0,255));
		TA_CONVERSATION.setLineWrap(true);
		TA_CONVERSATION.setRows(5);
		TA_CONVERSATION.setEditable(false);
		
		SP_CONVERSATION = new JScrollPane();
		SP_CONVERSATION.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		SP_CONVERSATION.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		SP_CONVERSATION.setViewportView(TA_CONVERSATION);
		MainWindow.getContentPane().add(SP_CONVERSATION);
		SP_CONVERSATION.setBounds(10, 90, 330, 180);
		
		L_ONLINE = new JLabel();
		L_ONLINE.setHorizontalAlignment(SwingConstants.CENTER);
		L_ONLINE.setText("Online Users");
		MainWindow.getContentPane().add(L_ONLINE);
		L_ONLINE.setBounds(350,70,130,16);
		
		JL_ONLINE = new JList();
		JL_ONLINE.setForeground(new java.awt.Color(0,0,255));
		JL_ONLINE.setFont(new java.awt.Font("Tahoma",0,15));
		
		SP_ONLINE = new JScrollPane();
		SP_ONLINE.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		SP_ONLINE.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		SP_ONLINE.setViewportView(JL_ONLINE);
		MainWindow.getContentPane().add(SP_ONLINE);
		SP_ONLINE.setBounds(350, 90, 130, 180);
		
		L_LoggedInAs = new JLabel();
		
		L_LoggedInAs.setFont(new java.awt.Font("Tahoma",0,12));
		L_LoggedInAs.setText("Logged in as ");
		MainWindow.getContentPane().add(L_LoggedInAs);
		L_LoggedInAs.setBounds(348, 0, 140, 15);
		
		L_LoggedInAsBox = new JLabel();
		L_LoggedInAsBox.setFont(new Font("Tahoma", Font.BOLD, 20));
		L_LoggedInAsBox.setForeground(Color.RED);
		L_LoggedInAsBox.setBackground(Color.WHITE);	
		L_LoggedInAsBox.setHorizontalAlignment(SwingConstants.CENTER);
		L_LoggedInAsBox.setBorder(null);
		MainWindow.getContentPane().add(L_LoggedInAsBox);
		L_LoggedInAsBox.setBounds(340, 17, 134, 48);
	}
	
	//--------------------------------------------------------------------------------------------
	
	public static void Login_Action()
	{
		
		B_ENTER.addActionListener(
				new ActionListener(){
				
				public void actionPerformed(java.awt.event.ActionEvent evt)
				{ ACTION_B_ENTER();
				
				}
				
			}	
				
				);	
	}
	
	//--------------------------------------------------------------------------------------------
	
	public static void ACTION_B_ENTER()
	{
		if (!TF_UserNameBox.getText().equals(""))
		{
			UserName = TF_UserNameBox.getText().trim();
			TF_UserNameBox.setText("");
			L_LoggedInAsBox.setText(UserName);
			MainWindow.setTitle(UserName + "'s Chat Box");
			LogInWindow.dispose();
			B_SEND.setEnabled(true);
			B_DISCONNECT.setEnabled(true);
			B_CONNECT.setEnabled(false);
			Connect();		
		}
		else
		{ 
			JOptionPane.showMessageDialog(null, "please enter a name!");		
		}
		
	}
	
	//---------------------------------------------------------------------------------------------
	
	public static void MainWindow_Action()
	{
		B_SEND.addActionListener(
				new ActionListener(){
					
					public void actionPerformed(java.awt.event.ActionEvent evt)
					{ ACTION_B_SEND();}
				}	
		);	
		

		B_DISCONNECT.addActionListener(
				new ActionListener(){
					
					public void actionPerformed(java.awt.event.ActionEvent evt)
					{ ACTION_B_DISCONNECT();}
				}	
		);	
		

		B_CONNECT.addActionListener(
				new ActionListener(){
					
					public void actionPerformed(java.awt.event.ActionEvent evt)
					{ BuildLogInWindow();}
				}	
		);	
		
	}
	
	//-------------------------------------------------------------------------------
	
	public static void ACTION_B_SEND()
	{
		if(!TF_Message.getText().equals(""))
		{
			ChatClient.SEND(TF_Message.getText());
			TF_Message.requestFocus();
		}	
	}
	
	//--------------------------------------------------------------------------------
	
	public static void ACTION_B_DISCONNECT()
	{
	
		try
		{
			B_ENTER.removeActionListener(null);
			ChatClient.DISCONNECT();
			B_CONNECT.setEnabled(true);
			B_DISCONNECT.setEnabled(false);
			Client_GUI.JL_ONLINE.setListData(new String[0]);
			L_LoggedInAsBox.setText("");
			
		}
		catch(Exception Y)
		{ 
			Y.printStackTrace();
		}	
	}
}
